package com.demo.my.Controller;

import com.demo.my.Entity.Department;
import com.demo.my.Entity.Employee;
import com.demo.my.Entity.Position;
import com.demo.my.Service.DepartmentService;
import com.demo.my.Service.EmployeeService;
import com.demo.my.Service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/showEmployee")
public class EmployeeListServlet extends HttpServlet {
    private EmployeeService employeeService=new EmployeeService();
    private PositionService positionService=new PositionService();
    private DepartmentService departmentService=new DepartmentService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Employee employee = null;
        try {
            employee = employeeService.getEmployeeById(id);
            Integer positionNumber = employee.getPositionNumber();
            Integer departmentNumber = employee.getDepartmentNumber();
            Department department = departmentService.getDepartment(departmentNumber);
            Position position = positionService.getPositionByNumber(positionNumber);
            req.setAttribute("position",position);
            req.setAttribute("department",department);
        } catch (Exception e) {
            e.printStackTrace();
        }
        req.setAttribute("employee",employee);
        req.getRequestDispatcher("/jsp/admin/employee_detail.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
